#!/usr/bin/env perl
#################################################################################
#
# The confidential and proprietary information contained in this file may
# only be used by a person authorised under and to the extent permitted
# by a subsisting licensing agreement from Jeejio Limited.
#
#                 (C) COPYRIGHT 2018-2019 JEEJIO Limited.
#                       ALL RIGHTS RESERVED
#
# This entire notice must be reproduced on all copies of this file
# and copies of this file may only be made by a person if such person is
# permitted to do so under the terms of a subsisting license agreement
# from JEEJIO Limited.
#
#	       hex2bin 
#	       =======
#
#
################################################################################

if ($#ARGV != 0) {
  print "\nUsage Format:  hex2bin xxx \n\n";
  die();
}
#-----delete the first input character when it including the "/" or "." and so on.
$_=$ARGV[0];
##s/\/\s*$//g; ## delete the "/" when the character including the "/"
s/\W+\s*$//g; ## delete the "/" when the character including the "/" or "." and so o
$inFileName = $_;
$outFileName=$inFileName."bin";
print "$inFileName \n"; 
print "$outFileName \n"; 

#----------------------------------------------------------
#Test the file exist or not. 
die "file $inFileName is not exist! \n" unless -e $inFileName;
#Open it.
open(IFILE,"<",$inFileName)  || die "open file $inFileName failed!\n" ;
open(OFILE,">",$outFileName) || die "open file $outFileName failed!\n";

################################################################################
# content begin
################################################################################

my $contents;
while($contents = <IFILE>)
{
  chomp($contents);
  $contents=~s/\s//g;#delete space
  for(my $j = 0; $j <= length($contents)-1; $j = $j + 2){
    my $sigu = pack("H*", substr($contents, $j, 2));
    syswrite(OFILE, $sigu);
  }
}

################################################################################
# content end 
################################################################################
#
print "hex2bin successfully!\n";

close(IFILE);
close(OFILE);
